home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter08
/
TranslateXYZPanel.java
< prev
Wrap
Text File
|
2000-06-27
|
2KB
|
108 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class TranslateXYZPanel extends Shout3DPanel implements DeviceObserver{
Transform boxTrans;
float[] worldPos = new float[3];
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
public TranslateXYZPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
boxTrans = (Transform) getNodeByName("mytrans");
worldPos = boxTrans.translation.getValue();
System.out.println("Box's x position is " + worldPos[0]);
System.out.println("Box's y position is " + worldPos[1]);
System.out.println("Box's z position is " + worldPos[2]);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//if left button used
if(mi.button == 0) {
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
float deltaX = dragDistanceX/50f;
float deltaY = -(dragDistanceY/50f);
worldPos[0] = worldPos[0] + deltaX;
worldPos[1] = worldPos[1] + deltaY;
boxTrans.translation.setValue(worldPos);
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
return true;
}//end of 0 if
//if right button used
if (mi.button == 1) {
pixelEndY = mi.y;
//get y pixel distances only
int dragDistanceY = pixelEndY - pixelStartY;
//vertical drag converted
//to 3D depth delta
float deltaZ = dragDistanceY/50f;
//add delta to current Z
worldPos[2] = worldPos[2] + deltaZ;
//put the updated position array
//in the transform node.
boxTrans.translation.setValue(worldPos);
//reset the starting pixel for next drag
pixelStartY = pixelEndY;
return true;
}//end of 1 if
}//end of switch
return false;
}
} //end of class